/**
 * FileName: MyDbHelper.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-18 下午3:22:15
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-18 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.utils;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import com.me.safebox.data.SqlUtil;
import com.me.safebox.service.FileConfig;
import com.me.safebox.service.FileHandleThreadPool;
import com.me.safebox.service.IFuture;
import com.me.safebox.ui.MainApp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @ClassName: MyDbHelper
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-18 下午3:22:15
 * 
 */
public class MyDbHelper extends DatabaseHelper {

	private static final String DB_NAME = "myDatabase2.db";
	private static String dbFile = FileConfig.SAFE_BOX_APP_DATABASE_PATH + File.separator + DB_NAME;

	private SQLiteDatabase db;

	public MyDbHelper() {
		super(MainApp.context, DB_NAME, null, 2);
	}

	@Override
	public File getDatabasePath(String name) {
		File file = new File(dbFile);
		if (!file.exists()) {
			try {
				File dir = new File(FileConfig.SAFE_BOX_APP_DATABASE_PATH);
				if (!dir.exists()) {
					if (!dir.mkdirs())
						return null;
				}
				file.createNewFile();
			} catch (IOException e) {
				Log.i("DB", e.getMessage());
			}
		}
		return file;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		this.db = db;
		db.execSQL(SqlUtil.SQL_CREATE_ENCRY_FILE_TABLE);
		db.execSQL(SqlUtil.SQL_CREATE_FOLDER_TABLE);
		// 新建数据库，第一次打开的时候调用
	}

	public SQLiteDatabase getSQLiteDatabase() {
		return this.getWritableDatabase();
	}

	public void insert(ContentValues values, String tableName) {
		db = this.getWritableDatabase();
		db.insert(tableName, null, values);
		db.close();
	} // 插入数据

	public void update(ContentValues values, String tableName, long id) {
		db = this.getWritableDatabase();
		db.update(tableName, values, "_id =?", new String[] { "" + id });
	}// 根据修改数据

	public Cursor query(String sql, String[] selectionArgs) {
		db = this.getWritableDatabase();
		Cursor c = db.rawQuery(sql, null);
		return c;
	}

	public Cursor query(String tableName) {
		db = this.getWritableDatabase();
		Cursor c = db.query(tableName, null, null, null, null, null, null);
		return c;
	}// 查询整个表，类似于遍历

	public void del(String tableName, long id) {
		if (db == null)
			db = this.getWritableDatabase();
		db.delete(tableName, "_id=?", new String[] { String.valueOf(id) });
	}// 删除数据

	public void close() {
		if (db != null)
			db.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		super.onOpen(db);
	}

	private static MyDbHelper mInstance = null;

	public static MyDbHelper getInstance() {
		if (mInstance == null) {
			synchronized (MyDbHelper.class) {
				if (mInstance == null) {
					mInstance = new MyDbHelper();
				}
			}
		}
		return mInstance;
	}
}
